package com.algorithm.base;

/**
 * 是字谜
 *
 * @author Amadeus
 * @date 2023/10/25
 */
public class ReverseString {

    /**
     * 翻转字符串1
     *
     * @param s s
     */
    public void reverseString(char[] s) {
        int length = s.length;
        for (int i = 0; i < length / 2; i++) {
            char f = s[i];
            s[i] = s[length - 1 - i];
            s[length - 1 - i] = f;
        }
    }

    /**
     * 翻转字符串2
     *
     * @param s s
     * @param k k
     * @return {@link String}
     */
    public static String reverseStr(String s, int k) {
        char[] chars = s.toCharArray();
        int length = chars.length;
        for (int i = 0; i < length; i += 2 * k) {
            change(chars, i, Math.min(i + k, length) - 1);
        }
        return String.valueOf(chars);
    }

    public static void change(char[] chars, int x, int y) {
        while (x < y) {
            char flag = chars[x];
            chars[x] = chars[y];
            chars[y] = flag;
            x++;
            y--;
        }
    }

    public static void main(String[] args) {
        System.out.println(reverseStr("abcdefg", 8));

    }


}
